//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/image/algorithm.h>
#include <buola/image/cimageplugin.h>
#include <buola/image/canyimage.h>

namespace buola { namespace img {

class CPGMCodec : public CCodec
{
public:
    virtual ~CPGMCodec() {}

    void Read(io::PBufferStream pSrc,CAnyImage &pDst,EImageFormat pFmt,const CSize_i &pSize);
    void Write(const CAnyView &pSrc,io::PBufferStream pDst);
};

class CPGMPlugin : public CImagePlugin
{
public:
    CPGMPlugin()
    {
        RegisterPlugin("pgm",this);
        RegisterPlugin("PGM",this);
        RegisterPlugin("ppm",this);
        RegisterPlugin("PPM",this);
    }

    CCodec *GetInstance()
    {
        return new CPGMCodec;
    }
};

static CPGMPlugin sPGMPlugin;

void CPGMCodec::Read(io::PBufferStream pSrc,CAnyImage &pDst,EImageFormat pFmt,const CSize_i& /*pSize*/)
{
    io::CTextReader lReader(pSrc);
    std::string lHeader;
    lReader >> lHeader;

    int lW,lH,lD;

    lReader >> lW >> lH >> lD;

    lReader.Ignore(1,'\n');

    if(lHeader=="P6")
    {
        CImage_rgb8 lDst({lW,lH});
        pSrc->Read(lDst.RawData(),lW*lH*3);
        pDst=std::move(lDst);
    }
    else if(lHeader=="P5")
    {
        CImage_gray8 lDst({lW,lH});
        pSrc->Read(lDst.RawData(),lW*lH);
        pDst=std::move(lDst);
    }
    else
    {
        throw XInvalid("invalid header in load_pgm");
    }
}

void CPGMCodec::Write(const img::CAnyView &pSrc,io::PBufferStream pDst)
{
    io::CTextWriter lWriter(pDst);

    if(pSrc.Format()==FORMAT_GRAY8)
    {
        lWriter << "P5\n";
        lWriter << pSrc.Width() << " " << pSrc.Height() << "\n255\n";

        CView_gray8 lSrc=pSrc.View<TFormat_gray8>();
        
        if(lSrc.Continuous())
        {
            pDst->Write(lSrc.RawData(),lSrc.Width()*lSrc.Height());
        }
        else
        {
            for(int i=0;i<lSrc.Height();i++)
                pDst->Write(lSrc.Row(i).RawData(),lSrc.Width());
        }
    }
    else if(pSrc.Format()==FORMAT_RGB8)
    {
        lWriter << "P6\n";
        lWriter << pSrc.Width() << " " << pSrc.Height() << "\n255\n";

        CView_rgb8 lSrc=pSrc.View<TFormat_rgb8>();

        if(lSrc.Continuous())
        {
            pDst->Write(lSrc.RawData(),lSrc.Width()*lSrc.Height()*3);
        }
        else
        {
            for(int i=0;i<lSrc.Height();i++)
                pDst->Write(lSrc.Row(i).RawData(),lSrc.Width()*3);
        }
    }
    else
    {
        throw XInternal("invalid format in write_pgm");
    }
}

/*namespace img*/ } /*namespace buola*/ }
